{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import datetime,shutil,schedule,time\n",
    "\n",
    "def job():\n",
    "    print(print(time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime())))\n",
    "    print('开始执行定时备份')\n",
    "    #pan = ['X']\n",
    "    pan = ['Z','Y','X']\n",
    "    for p in pan:\n",
    "        if p == 'Y': #第一次检查 49 wl电脑文件 HA\n",
    "            Y = r'W:\\HA\\Sun'#r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl'\n",
    "            YY ='HA'\n",
    "        elif p == 'Z':  #第二次检查 46 cak电脑文件 CAK \n",
    "            Y = r'Z:'\n",
    "            YY = 'CAK'\n",
    "        elif p == 'X':  #第三次检查 49 wlpco1我  电脑文件20210530添加  WL\n",
    "            Y = r'X:'\n",
    "            YY = 'WL'\n",
    "\n",
    "        path37 = r'\\\\10.153.110.37\\Data\\solar_observation'\n",
    "        filelie=[]#文件列表\n",
    "        for root,dirs,files in os.walk(Y):\n",
    "            for i in dirs:\n",
    "                if len(i) == 13:  #针对10/20之前的文件命名\n",
    "                    times ='20'+i[4:6]+i[2:4]+i[0:2]\n",
    "                    #times=datetime.datetime.strptime(times,'%Y%m%d')\n",
    "                    filelie.append(times)\n",
    "                elif len(i) >13:  #新的文件命名方式\n",
    "                    filelie.append(i[:8])\n",
    "                elif len(i) == 8:  #wlpco 2021.5.30添加\n",
    "                    filelie.append(i[:8])\n",
    "        #对文件夹进行排列\n",
    "        filelie.sort(reverse=True)\n",
    "\n",
    "        file7 =[] #输出最近7天观测时间 20201203\n",
    "        file7bf=[] #备份文件名称 20201203\n",
    "\n",
    "        #如需修改观测时间列表长度，则修改for i in filelie[:7]尾部[:7]的7，需要改成10则是[:10]\n",
    "        print(Y +'盘最近10次观测的时间列表')\n",
    "        for i in filelie[:10]:\n",
    "            file7.append(i)\n",
    "            print(i)\n",
    "        print('正在检查是否上传至37网盘')\n",
    "        for i in file7:\n",
    "            if os.path.exists(os.path.join(path37,i[:4],YY.lower(),i+YY)):\n",
    "                print(i+'存在，不再进行备份')  #20201203\n",
    "            else:\n",
    "                print(i+'文件还未备份，开始创建备份')\n",
    "                file7bf.append(i)  #20201203\n",
    "\n",
    "        os.chdir(Y)\n",
    "        for bf in set(file7bf):  #获得需要备份文件夹名称\n",
    "            for i in set(os.scandir()): #获得原本文件夹名称\n",
    "                if bf in i.name:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
    "                    try:\n",
    "                        print('开始备份1，请稍后')\n",
    "                        print(i.name)\n",
    "                        move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                        地址 = os.path.join(r'\\\\10.153.110.37\\Data\\solar_observation',i.name[:4],YY.lower())\n",
    "                        shutil.copytree(move,地址+'\\\\'+i.name[:8]+YY) #开始备份\n",
    "                        print(i.name[:8]+'备份完成')\n",
    "                    except FileExistsError:  \n",
    "                        for filepath in os.scandir(os.path.join(Y,i.name)):\n",
    "                            shutil.copy2(filepath.path,地址+'\\\\'+i.name[:8]+YY)\n",
    "                        print(i.name[:8]+'备份完成1')\n",
    "                    except SameFileError:\n",
    "                        pass\n",
    "\n",
    "                elif bf[6:8]+bf[4:6]+bf[2:4] in i.name: #针对2020年10月24日之前的文件命名\n",
    "                    try:\n",
    "                        print('开始备份2，请稍后')\n",
    "                        move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                        shutil.copytree(move,地址+'\\\\'+bf+YY) #开始备份\n",
    "                        print('备份完成')\n",
    "                    except:\n",
    "                        pass\n",
    "                    \n",
    "schedule.every().day.at(\"18:10\").do(job)\n",
    "#schedule.every(5).hours.at(\"11:11\").do(job)\n",
    "\n",
    "while True:\n",
    "    schedule.run_pending()\n",
    "    #time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
